using Godot;
using System;

public partial class 动画播放节点2 : AnimationPlayer
{
    public Sprite2D sprite;

    public float gravity = 800;
    public float hspeed = 100;

    public float vspeed = 100;
    

    public float groundY = 0f;

    public float jumpHeightTo = -500;

    public float velocityY = 0f;

    public bool isOnGround = true;

    public override void _Ready()
    {
        Sprite2D node = GetChildOrNull<Sprite2D>(0);
        if (node != null) sprite = node;
        Stop();
        groundY = sprite.Position.Y;

    }


    public override void _Process(double delta)
    {
        Vector2 position = sprite.Position;
        var running = false;
        if (Input.IsActionPressed("x"))
        {
            sprite.FlipH = false;
            running = true;
            sprite.Position = new Vector2(position.X - hspeed * (float)delta, position.Y);
        }
        if (Input.IsActionPressed("c"))
        {
            sprite.FlipH = true;
            running = true;
            sprite.Position = new Vector2(position.X + hspeed * (float)delta, position.Y);
        }

        if (Input.IsActionPressed("空格") && isOnGround)
        {

            isOnGround = false;
            velocityY = jumpHeightTo;
            
        }

        if (!isOnGround)
        {
            velocityY += gravity * (float)delta;
            sprite.Position = new Vector2(sprite.Position.X, sprite.Position.Y + velocityY * (float)delta);
            if (sprite.Position.Y >= groundY)
            {
                sprite.Position = new Vector2(sprite.Position.X, groundY);
                velocityY = 0f;
                isOnGround = true;
            }
            running = true;
        }

        if (running)
        {
            if (!IsPlaying()) Play("Move");
        }
        else
        {
            if (IsPlaying()) Stop();
        }
    }

}
